package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 十四、一般健康问卷（Ghq）共28题
 */
public class Table14 extends AbstractTable implements TableCalculate{
    String code = "Ghq";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetGhqFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return null;
    }

    private static ClsAnswerResult GetGhqFactorScore(ClsAnswerRequest answer) {
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 28;
            // '1生理健康，2精神心理健康，3社会活动与工作能力，4生活态度及前途，5总分
            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;
            // 共一组，1,2,3,4

            // 诊断结果
            String[] vAppraiseA = new String[5];

            int[] vFactorA = new int[5]; // 实际为5个因子
            for (i = 0; i < vFactorA.length; i++) {
                vFactorA[i] = 0;
            }

            // 原始分
            for (i = 0; i < 4; i++) {
                for (j = 0; j < 7; j++) {
                    vFactorA[i] += Integer.valueOf(sAnswer[i * 7 + j]);
                }
                // 总分
                vFactorA[4] += vFactorA[i];
            }

            // 诊断结果
            // 前4个因子{7,14,21}，总分{28,56,84}
            int[][] intValueA = { { 7, 14, 21 }, { 28, 56, 84 } };
            String[][] strValueA = { { "好", "正常", "有中度问题", "有重度问题" }, { "健康", "基本健康", "有健康问题", "有严重健康问题" } };
            int intLocal = 0;
            for (i = 0; i < vFactorA.length; i++) {
                if (i >= 4) {
                    intLocal = 1;
                }
                vAppraiseA[i] = strValueA[intLocal][strValueA[1].length - 1];
                for (j = 0; j < intValueA[1].length; j++) {
                    if (vFactorA[i] <= intValueA[intLocal][j]) {
                        vAppraiseA[i] = strValueA[intLocal][j];
                        break;
                    }
                }
            }

            ClsScore.Ghq ghq = new ClsScore.Ghq();

            ghq.setSljk_o(vFactorA[0]);
            ghq.setSljk_a(vAppraiseA[0]);

            ghq.setJsxl_o(vFactorA[1]);
            ghq.setJsxl_a(vAppraiseA[1]);

            ghq.setShhd_o(vFactorA[2]);
            ghq.setShhd_a(vAppraiseA[2]);

            ghq.setShtd_o(vFactorA[3]);
            ghq.setShtd_a(vAppraiseA[3]);

            ghq.setTotal_o(vFactorA[4]);
            ghq.setTotal_a(vAppraiseA[4]);

            result.setObject(ghq);
            return result;

        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }


}
